//
//  StoneStreetDetailViewController.m
//  Globalstone
//
//  Created by 刘建峰 on 16/7/19.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "StoneStreetDetailViewController.h"
#import "SureIndentViewController.h"


@interface StoneStreetDetailViewController ()<BestScrollViewDelegate,BestScrollViewDatasource>
@property (weak, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UIButton *collectionButton;
@property (nonatomic,strong)  BestScrollView *headerScroll;//滚动栏
@property (nonatomic,strong)  NSMutableArray  *imageArray;
@property (weak, nonatomic) IBOutlet UILabel *goodsName;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *entityLabel;
@property (weak, nonatomic) IBOutlet UILabel *colorLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftNum;

@property (weak, nonatomic) IBOutlet UILabel *texture;
@property (weak, nonatomic) IBOutlet UILabel *feature;
@property (weak, nonatomic) IBOutlet UILabel *productLand;

@property(nonatomic,strong)NSDictionary * productDic;
@end

@implementation StoneStreetDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageArray = [NSMutableArray array];
    [self initViewLayout];
    [self getShopDetail];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.scroView];
    
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 527), NO);
    
    _headerScroll = [[BestScrollView alloc]  initWithFrame:flexibleFrame(CGRectMake(0, 64, 320, 155), NO)];
    [self.scroView insertSubview:_headerScroll belowSubview:_backButton];
}

- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark === 获取产品详情
- (void)getShopDetail{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    [dic setObject:_stoneId forKey:@"mgooId"];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryMallGoodsDetail withHttpType:@"POST" withTag:GS_GS_queryMallGoodsDetail];
}

#pragma mark === 收藏
- (IBAction)collectHandel:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    [dic setObject:_stoneId forKey:@"mgooId"];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    
    self.shareApi.delegate=self;
    
    if (!sender.selected) {
        [self.shareApi  requestOperationWithDic:dic withPath:GS_mallCollectGoods withHttpType:@"POST" withTag:GS_GS_mallCollectGoods];
    }else{
        [self.shareApi  requestOperationWithDic:dic withPath:GS_canleMallCollectGoods withHttpType:@"POST" withTag:GS_GS_canleMallCollectGoods];
    }

}

#pragma mark === 加入购物车
- (IBAction)putInshoppingCar:(id)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    [dic setObject:_stoneId forKey:@"mgooId"];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_addCartByMgooId withHttpType:@"POST" withTag:GS_GS_addCartByMgooId];


}

#pragma mark === 立即购买
- (IBAction)buyNow:(id)sender {
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
    SureIndentViewController * page = [storyBoard instantiateViewControllerWithIdentifier:@"SureIndent"];
    page.productDic = [_productDic mutableCopy];
    page.storeName = _storeName;
    page.storeId = _storeId;
    page.transPrice = _transPrice;
    [[Common getNavigation]pushViewController:page];

}

#pragma  mark BestView Delegate Data=source=====================


- (NSInteger)numberOfPages
{
    
    return  self.imageArray.count;
    
}

- (UIView *)pageAtIndex:(NSInteger)index
{
    
    if (self.imageArray.count>0) {
        
        UIImageView  *imageView=[[UIImageView alloc]  initWithFrame: flexibleFrame(CGRectMake(0, 0, 320, 140), NO)];
        [imageView  sd_setImageWithURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,_imageArray[index][@"msimImage"]]] placeholderImage:[UIImage imageNamed:@"beijing"]];
        return imageView;
    }
    return nil;
}


- (void)didClickPage:(BestScrollView *)csView atIndex:(NSInteger)index
{
}



#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == GS_GS_queryMallGoodsDetail){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            
            self.imageArray =database[@"result"][@"images"];
            if (_imageArray.count==0) {
                return;
            }
            self.headerScroll.delegate=self;
            self.headerScroll.datasource=self;
            [self.headerScroll showImageArray:self.imageArray   withAnimation:YES];
            
            NSDictionary * dic = database[@"result"];
            _productDic = dic;
            NSString * collect = [NSString stringWithFormat:@"%@",dic[@"mgooIsCollect"]];
            if ([collect isEqualToString:@"1"]) {
                _collectionButton.selected = YES;
            }else{
                _collectionButton.selected = NO;
            }
            
            _goodsName.text = [NSString stringWithFormat:@"%@",dic[@"mgooName"]];
            _priceLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"mgooPrice"]];
            _entityLabel.text = [NSString stringWithFormat:@"%@",dic[@"mgooSpecify"]];
            _colorLabel.text = [NSString stringWithFormat:@"%@",dic[@"mgooColor"]];
            _leftNum.text = [NSString stringWithFormat:@"%@",dic[@"mgooStockNum"]];
            _texture.text = [NSString stringWithFormat:@"%@",dic[@"mgooFigure"]];
            _feature.text = [NSString stringWithFormat:@"%@",dic[@"mgooFeature"]];
            _productLand.text = [NSString stringWithFormat:@"%@",dic[@"mgooOrigin"]];
        }
    }else if (tag == GS_GS_addCartByMgooId){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
             [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
        }
    }if (tag == GS_GS_mallCollectGoods){
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            _collectionButton.selected = YES;
        }
        
    }else if (tag == GS_GS_canleMallCollectGoods){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            _collectionButton.selected = NO;
        }
        
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
